package com.yonyou.pmclouds.billview.plugin;

import com.yonyou.pmclouds.alterandvisa.entity.AlterAndVisaVO;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.businessobject.util.BusinessObjectConsts;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class VisaViewPlugin implements IBillViewCustomPlugin {

    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Autowired
    private UserInfoQuery userInfoQuery;

    @Override
    public Object[] fillListBuziField(Object[] objects) throws BusinessException {

        Set<String> constructionUnitPks = new HashSet<>();

        AlterAndVisaVO[] alterAndVisaVOs = new AlterAndVisaVO[objects.length];

        for (int i = 0; i < objects.length ; i++){
            alterAndVisaVOs[i] = (AlterAndVisaVO)objects[i];
        }

        // 取到所有施工单位的pk,获取施工单位信息
        for (AlterAndVisaVO alterAndVisaVO : alterAndVisaVOs) {
            constructionUnitPks.add(alterAndVisaVO.getPkEnterprise());
        }
        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(constructionUnitPks.toArray(new String[0]));
        for (AlterAndVisaVO alterAndVisaVO : alterAndVisaVOs) {
            EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(alterAndVisaVO.getPkEnterprise());
            alterAndVisaVO.setEnterpriseName(enterpriseInfo==null?"":enterpriseInfo.getName());
        }
        return alterAndVisaVOs;
    }

    @Override
    public Map<String,Object[]> fillDetailBuziField(SuperVO superVO, HttpServletRequest request) throws BusinessException {

        userInfoQuery.tranUserInfoField(new Object[]{superVO},new String[]{CommonFieldConst.CREATOR},new String[]{AlterAndVisaVO.CREATOR_NAME});

        String pkEnter = BeanHelper.getProperty(superVO,"pkEnterprise") == null?null:(String)BeanHelper.getProperty(superVO,"pkEnterprise");

        Map<String, EnterpriseInfo> enterpriseInfoMap = enterpriseInfoQuery
                .queryEnterpriseByIds(new String[]{pkEnter});
        EnterpriseInfo enterpriseInfo = enterpriseInfoMap.get(pkEnter);
        BeanHelper.setProperty(superVO,"enterpriseName",enterpriseInfo==null?"":enterpriseInfo.getName());

        Map<String,Object[]> resultMap = new HashMap<String,Object[]>();

        resultMap.put(BusinessObjectConsts.PM_ALTERANDVISA,new Object[]{superVO});

        return resultMap;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.ALTER_AND_VISA_BILLTYPE;
    }
}
